//
//  AppDelegate.m
//  01-UIView、UIWindow
//
//  Created by QC.L on 16/7/14.
//  Copyright © 2016年 QC.L. All rights reserved.
//

#import "AppDelegate.h"
#import "UIView+Frame.h"
@interface AppDelegate ()<UITextFieldDelegate>

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    // Override point for customization after application launch.
    /***程序的组成结构
     * 1.main.m 主要实现了程序的正常运行
     * 2.AppDelegate 程序的执行者, 签订了UIApplicationDelegate(协议方法可选)
     * 注意: APPDelegate就是一个普通的类, 只不过签订协议, 才有这么多方法
     * 3.ViewController 视图控制器 主要控制视图管理
     * 4.Main.storyboard(视图管理) LaunchScreen.storyboard(负责启动页) 可视化管理(Xcode 7 以前叫 LaunchScreen.xib)
     * 5.Assets.xcassets 主要用来管理图片素材(Xcode7以前叫Images.xcassets)
     * 6.Info.plist (工程配置文件)
     */

    /***模拟器的操作
     * command + 1~5 改变模拟器的大小
     * command + ←/→ 改变模拟器的横屏/竖屏
     */
    
    /***iOS概述
     * 正确的写法:iOS 错误的写法:IOS ios Ios IoS
     */
    
    /***1.UIWindow (程序中, 只有一个主window)
     * 创建前需要做的操作
     *  1> 取消程序的可视化路口, Main.storyboard
     *  2> 创建主window对象
     */
    // 1.创建主window, 使其大小与屏幕一样
    // UIScreen 系统的屏幕类
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    // 2.给你创建的窗口赋予颜色 UIColor 系统颜色类
    _window.backgroundColor = [UIColor whiteColor];
    // 使Window显示, 并成为主窗口
    [_window makeKeyAndVisible];
    // 3.创建一个视图控制器
    UIViewController *vc = [[UIViewController alloc] init];
    // 4.给window指定根视图控制器(Xcode7之后, 必须设置rootViewController, 否则, 在该方法结束时, 会崩溃)
    _window.rootViewController = vc;
    // 3> UIWindow 继承于 UIView
      /***2.UIView(与NSObject相似, 是UI阶段的根类, 大部分UI控件继承于UIView)
     * 学习新的类
     *   1> 首先应该知道新类包含哪些初始化方法
     * 创建UIView
     */
#if 0
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 300, 300)];
    // 设置view的背景颜色
    view.backgroundColor = [UIColor cyanColor];
    // 添加到window上
    [_window addSubview:view];
    // 遇到UIView的子类, 都可以用以上的方式创建和显示
    // UIView在屏幕中显示的是矩形
    
    /* iOS系统坐标系
     * 1.与数学中的坐标系不同, Y轴的正方向是向下的
     * 2.原点是屏幕的左上角
     * 3.frame bounds center (都是UIView的属性)
     * 1)frame (x, y, width, height) 视图在其父视图上的位置
     * 2)bounds (x, y, width, height) 视图本身的坐标系(0, 0, width. height)
     * 3)center 中心点 改变中心点(影响视图的位置)
     */
    //    view.center = self.window.center;
//    view.frame = CGRectMake(10, 10, 100, 100);
    // bounds 对视图影响的测试
    // bounds 的x和y只影响子视图的坐标(因为它改变的是坐标系),
    // bounds 的width和height修改的是本身的大小, 会以中心点收缩/扩张.
    // bounds 的改变不会改变该视图的位置
//    NSLog(@"改变前%@", NSStringFromCGRect(view.frame));
//    NSLog(@"改变前的center%@", NSStringFromCGPoint(view.center));
//    view.bounds = CGRectMake(0, 0, 200, 200);
//    NSLog(@"改变后%@", NSStringFromCGRect(view.frame));
//    NSLog(@"改变后的center%@", NSStringFromCGPoint(view.center));
    UIView *subView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 50, 50)];
    subView.backgroundColor = [UIColor redColor];
    [view addSubview:subView];
// 改变view的大小, width变成100
//    view.frame get方法
//    frame.size 结构体
//    OC和C的内容结合, 是一个常量
//    CGRect rect = view.frame;
//    rect.size.width = 100;
//    view.frame = rect;
    
    view.frame = CGRectMake(0, 0, 100, 300);
    NSLog(@"view的大小%@", NSStringFromCGRect(view.frame));
//   CGRect和CGPoin
    view.dm_height = 100;
#endif
#pragma  mark - UIView的基本属性
#if 0
    UIView *testView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    // 1.背景颜色
    testView.backgroundColor = [UIColor redColor];
    [_window addSubview:testView];
    // 2.alpha 透明度(0.0 ~ 1.0) 默认值是1.0
    testView.alpha = 0.3;
    // 3.tag
    testView.tag = 100;
    // 根据tag获取到对应的view(用UIView接收) __kindof
    UIView *view = [_window viewWithTag:100];
    NSLog(@"%@", view);
    // 4.subViews(用UIView接收)
    UIView *view1 = [_window.subviews objectAtIndex:0];
    NSLog(@"%@", view1);
    // 5.superView
    UIView *superView = [view superview];
    NSLog(@"superView =%@", superView);
    // 6.hidden 隐藏/显示 (YES隐藏, NO显示)
    testView.hidden = NO;
#endif
#pragma mark - 视图的层级
#if 0
    UIView *firstView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 80, 80)];
    firstView.backgroundColor = [UIColor orangeColor];
    UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(20, 20, 80, 80)];
    secondView.backgroundColor = [UIColor blueColor];
    UIView *threeView = [[UIView alloc] initWithFrame:CGRectMake(30, 30, 80, 80)];
    threeView.backgroundColor = [UIColor redColor];
    // addSubview (添加视图)
    [_window addSubview:firstView];
    // insertSubview:atIndex(插入视图到指定下标)
    NSLog(@"%@", _window.subviews);
    [_window insertSubview:secondView atIndex:3];
    // insertSubview:aboveSubview(插入视图到指定视图的上面)
    [_window insertSubview:threeView aboveSubview:secondView];
    // insertSubview:belowSubview(插入视图到指定视图的下面)
    [_window insertSubview:firstView belowSubview:threeView];
#endif
#pragma mark - UILabel(负责显示文本)
#if 0
    // 若果类没有自己的初始化方法, 则使用父类
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(50, 150, 300, 70)];
    label.backgroundColor = [UIColor yellowColor];
    [_window addSubview:label];
    // UILabel 的属性
    // 1.text
    label.text = @"撒的奇偶死哦技术的积分穷手机及搜救方is就覅偶即搜附近iOS减肥欧就覅偶今晚就是id覅偶时间的费时间的覅就死哦近似计算的积分束带结发设计的覅圣诞节覅时间地欧锦赛的if死哦的囧束带结发欧is大姐夫设法就是靠劳动纠纷路口安吉斯接受";
    // 2.textColor (改变UILabel文本的字体颜色)
    label.textColor = [UIColor redColor];
    // 3.font (改变文本字体大小)默认值是17
    label.font = [UIFont systemFontOfSize:20];
    // font的构造器类型(加粗字体)
    label.font = [UIFont boldSystemFontOfSize:15];
    // 4.textAlignment 对齐方式(有好几种, 一调就知道) 默认对齐方式NSTextAligmentLeft
    label.textAlignment = NSTextAlignmentCenter;
    // 5.numberOfLines(行数) 默认为一行, 不确定行数时给值为0
    label.numberOfLines = 0;
    // lineBreakMode 换行模式, ...(省略号)的位置
    label.lineBreakMode = NSLineBreakByTruncatingMiddle;
    // 阴影
    label.shadowColor = [UIColor lightGrayColor];
    label.shadowOffset = CGSizeMake(2, 1);
#endif
#pragma mark - UIButton
#if 1
    // UIButtonTypeSystem 系统的按钮类型
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.backgroundColor = [UIColor purpleColor];
    button.frame = CGRectMake(157, 100, 100, 50);
    [_window addSubview:button];
    // 给button添加事件
    [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // 给button添加文字
    [button setTitle:@"点击有惊喜" forState:UIControlStateNormal];
    //    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
#endif
#pragma mark - UITextField
#if 1
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(107, 200, 200, 50)];
    textField.backgroundColor = [UIColor cyanColor];
    [_window addSubview:textField];
    // 1.给输入框赋值
//    textField.text = @"请输入账号";
    textField.textAlignment = NSTextAlignmentCenter;
    // 2.文字颜色
    textField.textColor = [UIColor redColor];
    // 3.borderStyle 边缘样式
    // UITextBorderStyleNone 无样式
    // UITextBorderStyleLine 有边框
    // UITextBorderStyleBezel 有立体感的边框
    // UITextBorderStyleRoundedRect 圆角
    textField.borderStyle = UITextBorderStyleRoundedRect;
    // 4.占位字符(当text不为空时, placeholder是不显示的)
    textField.placeholder = @"请输入水电费账号";
    // clearsOnBeginEditing (当重新编辑的时候, 清空textField中的内容)
    textField.clearsOnBeginEditing = YES;
    // command + k 显示/收起键盘
    textField.delegate = self;

    
    
#endif
    return YES;
}
- (void)buttonClicked:(UIButton *)button {
    // 结束编辑
    [_window endEditing:YES];
    NSLog(@"Surprised!");
}
// 开始编辑前, 判断能否进行编辑(返回值为YES可以编辑)
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    return YES;
}
// 判断是否结束编辑
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
    return YES;
}
// 已经开始编辑时
- (void)textFieldDidBeginEditing:(UITextField *)textField {
    
}
// 已经结束编辑时
- (void)textFieldDidEndEditing:(UITextField *)textField {
    
}
// 当点击return是触发
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    // 处理点击return, 需要进行的操作
    // 回收键盘
    [textField endEditing:YES];
    return YES;
}

- (BOOL)textFieldShouldClear:(UITextField *)textField {
    
    return YES;
}
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
